<?php

class Pedido_ReservaController extends Zend_Controller_ActionAquiles {

    public function init() {
        parent::init();
        $this->view->title = 'Pedidos';
    }

    public function indexAction() {
        
    }
    
    public function saveAction() {
        $model = Application_Model_Reserva::getInstance();
        $idReservas = $_REQUEST['idreservas'];
        if(isset ($idReservas) && !empty ($idReservas)){
            $model->update($this->getReserva());
        }else{
            $model->insert($this->getReservaNovo());
        }
        
        return $this->_helper->redirector('index','consulta', 'reserva');
    }
    
    public function excluiAction(){
        $model = Application_Model_Reserva::getInstance();
        $filter = new Zend_Filter_Date();
        $mesa = $_REQUEST['mesa'];
        $data = $_REQUEST['data'];
        
        if((!isset($mesa) || empty($mesa)) ||  (!isset($data) || !Zend_Date::isDate($data)) ){
            return $this->_helper->redirector('index','consulta', 'reserva');
        }
        
        Application_Model_Reserva::getInstance()->removeByDataMesa( $filter->filter($data),$mesa);
        return $this->_helper->redirector('index','consulta', 'reserva');
    }
    
    public function editaAction(){
        $filter = new Zend_Filter_Date();
        $mesa = $_REQUEST['mesa'];
        $data = $_REQUEST['data'];
        
        if((!isset($mesa) || empty($mesa)) ||  (!isset($data) || !Zend_Date::isDate($data)) ){
             return $this->_helper->redirector('index','consulta', 'reserva');
        }
        
        $reserva = Application_Model_Reserva::getInstance()->getByDateMesa( $filter->filter($data),$mesa);
        if(isset ($reserva) && count($reserva) > 0){
            $reserva[0]['data'] = $filter->filter($reserva[0]['data']);
            $reserva = $reserva[0];
        }else{
            $reserva['nome'] = '';
            $reserva['telefone'] = '';
            $reserva['data'] = $data;
            $reserva['hora'] = '';
            $reserva['idreservas'] = '';
            $reserva['mesa'] = $mesa;
        }
        
        
        $this->view->mesa = $mesa;
        $this->view->data = $data;
        $this->view->reserva = $reserva;
        
    }
    
    public function validaAction() {
        $resp = new Util_Function_JsonResponse();
        echo Zend_Json::encode(array("erros" => $resp->getErros()), Zend_Json::TYPE_OBJECT);
        die();
    }
    
    private function getReserva(){
        $filter = new Zend_Filter_Date();
        $reserva['nome'] = $_POST['nome'];
        $reserva['telefone'] = $_POST['telefone'];
        $reserva['data'] = $filter->filter($_POST['dia']);
        $reserva['hora'] = $_POST['hora'];
        $reserva['mesa'] = $_POST['mesa'];
        $reserva['idreservas'] = $_POST['idreservas'];
        return $reserva;
    }
    
    private function getReservaNovo(){
        $filter = new Zend_Filter_Date();
        $reserva['nome'] = $_POST['nome'];
        $reserva['telefone'] = $_POST['telefone'];
        $reserva['data'] = $filter->filter($_POST['dia']);
        $reserva['hora'] = $_POST['hora'];
        $reserva['mesa'] = $_POST['mesa'];
        return $reserva;
    }

}